<?php
class Page extends SiteTree {

    public static $db = array(
        'ShowBreadcrumbs' => 'Boolean'
    );

    public static $has_one = array(
        'Image' => 'Page_Image'
    );
    
    public static $defaults = array(
        'ShowBreadcrumbs' => '1'
    );
    
    static $icon = 'themes/default/images/icons/page';
    
    static $allowed_children = array("*Page", "ShopPage", "VirtualPage", "RedirectorPage", "UserDefinedForm");
    
    public function getCMSFields() {   
        $fields = parent::getCMSFields(); 
        $fields->addFieldToTab('Root.Content.Main', new ImageField('Image', 'Afbeelding toevoegen'), 'Content');
        $fields->addFieldToTab('Root.Content.Main', new CheckboxField('ShowBreadcrumbs', 'Breadcrumbs tonen'));
        return $fields;
    }  

}
class Page_Controller extends ContentController {

    public static $allowed_actions = array (
    );

    public function init() {
	parent::init();
        
        Requirements::javascript(PROJECT_DIR . '/javascript/jquery-1.6.2.min.js');
        Requirements::javascript(PROJECT_DIR . '/javascript/jquery.superbox-min.js');
        Requirements::customScript('
        
        (function($) {
            $(document).ready(function(){   
            
            $.superbox.settings = {
                boxId: "superbox", // Id attribute of the "superbox" element
                boxClasses: "", // Class of the "superbox" element
                overlayOpacity: .8, // Background opaqueness
                boxWidth: "600", // Default width of the box
                boxHeight: "400", // Default height of the box
                loadTxt: "Bezig met laden...", // Loading text
                closeTxt: "Sluiten", // "Close" button text
                prevTxt: " ", // "Previous" button text
                nextTxt: " " // "Next" button text
            };        
            $.superbox();
            
            })
        })(jQuery);
        
        ');
    }
    
    public function getRandomPremium() {
        
        return Dataobject::get("Shop", "IsPremium = '1'", "RAND()", "", 1);
    }
    
    public function getLatestNews() {
        
        return Dataobject::get("ArticlePage", "", "Date DESC", "", 3);
    }
    
    public function results($data, $form){
        $data = $_REQUEST; 
        $SQL_start = (int)$_GET['start'];
        
        $query = htmlspecialchars($data['Search'], ENT_QUOTES,'UTF-8'); 
        
        $shops = DataObject::get("Shop","MATCH (Name,Description) AGAINST ('$query' IN BOOLEAN MODE)");
        $pages = DataObject::get("SiteTree","MATCH (Title,Content) AGAINST ('$query' IN BOOLEAN MODE)");
        
        $searchresults = new DataObjectSet();
        $searchresults->merge($shops);
        $searchresults->merge($pages);
        
        $o = $searchresults->getRange($SQL_start,10);
        $o->setPageLimits($SQL_start, 10, $searchresults->Count()); 
         
        if($o){ 
            $data['Results'] = $o;           
        } else { 
            $data['Results'] = ''; 
        } 
        
        $data['Title'] = 'Zoekresultaten'; 

        return $this->customise($data)->renderWith(array('Page_results','Page')); 
    }
    
    
     
}

class Page_Image extends Image {
    
    public function generateResized($gd) {
        return $gd->resize(169,169);	
    }
}